﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common;

namespace Qianwei.Web.Admins
{
    public partial class EditPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string CheckInput()
        {
            string strErr = string.Empty;
            if (txtCurrentPwd.Text.Trim().Length == 0)
            {
                strErr = "请输入当前密码";
            }
            else if (txtNewPwd.Text.Trim().Length == 0)
            {
                strErr = "请输入新密码";
            }
            else if (txtPwdAgain.Text.Trim().Length == 0)
            {
                strErr = "请输入确认密码";
            }
            else if (txtNewPwd.Text.Trim() != txtPwdAgain.Text.Trim())
            {
                strErr = "新密码两次输入不一致";
            }
            else if (!UserInfoManager.Instance.UserIsExists(User.Identity.Name, Util.MD5Encrypt(txtCurrentPwd.Text.Trim())))
            {
                strErr = "当前密码输入不正确";
            }
            return strErr;
        }

        private void SavePwd()
        {
            user_info userInfo = new user_info();
            userInfo.Login_Name = User.Identity.Name;
            userInfo.Password = Util.MD5Encrypt(txtNewPwd.Text.Trim());

            if (UserInfoManager.Instance.UpdatePassword(userInfo))
            {
                Util.PageAlert("更改密码成功");
            }
            else
            {
                Util.PageAlert("对不起,更改密码失败");
            }
        }
        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string result = CheckInput();
            if (result.Length > 0)
            {
                Util.PageAlert(result);
            }
            else
            {
                SavePwd();
            }
        }
        #endregion
    }
}